package com.nineshadow.games.core.step;

import cn.stylefeng.guns.modular.system.controller.BixingInterfaceController;
import cn.stylefeng.guns.modular.system.model.GameUser;
import com.nineshadow.games.Enum.GameConfigEnum;
import com.nineshadow.games.constants.CommonConstants;
import com.nineshadow.games.core.GameRoom;
import com.nineshadow.games.interfaces.IStep;
import com.nineshadow.games.launch.HallServer;
import com.nineshadow.games.util.MyUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@Slf4j
public class Step1Start implements IStep {

    /**
     * 准备倒计时3s
     */
    private static final int READY_TIME = 3;

    @Override
    public void start(GameRoom room) {
        log.info("游戏开始：{}", room.SubscribeTab);
        //创建语音房间，并开始游戏
        GameUser gameUser = room.users.get(0);
        GameUser gameUser1 = room.users.get(1);
        String voiceRommId="";
        if(StringUtils.isNotEmpty(getCacheVoiceRoom(gameUser.getId()))&&StringUtils.isNotEmpty(getCacheVoiceRoom(gameUser1.getId()))){
            voiceRommId=getCacheVoiceRoom(gameUser.getId());
        }else{
            String openIds = gameUser.getOpenId() + "," + gameUser1.getOpenId();
            try {
                voiceRommId= String.valueOf(BixingInterfaceController.voiceRoom(openIds,room.getGameTypeEnum()));
            }catch (Exception e){
                log.error(e.getMessage(),e);
            }
        }
        //推送开始游戏
        Map<String, Object> map = new HashMap<>();
        map.put("voiceRoomId", voiceRommId);
        HallServer.redisUtil.hset(CommonConstants.USER + gameUser.getId(), CommonConstants.USER_VOICE_ROOM, voiceRommId);
        HallServer.redisUtil.hset(CommonConstants.USER + gameUser1.getId(), CommonConstants.USER_VOICE_ROOM, voiceRommId);
        HallServer.publishService.publish(room.SubscribeTab, gameUser.getId(),MyUtils.putMsg("start", map));
        HallServer.publishService.publish(room.SubscribeTab, gameUser1.getId(),MyUtils.putMsg("start", map));
        MyUtils.sleepSomeMillis(READY_TIME * 1000);

        //每10s 推一次给前端同步时间
        int gameTime=120;
        String time=HallServer.gameConfigService.getConfig(GameConfigEnum.TIME);
        if(StringUtils.isNotEmpty(time)){
            gameTime=Integer.parseInt(time);
        }
        while (!room.isTerminated()&&gameTime>=0){
            if(gameTime%10==0){
                room.sendStepTime(gameUser.getId(),gameTime);
                room.sendStepTime(gameUser1.getId(),gameTime);
            }
            gameTime--;
            MyUtils.sleepSomeMillis(1000);
        }
        setNextStepClass(room, Step2Over.class);
    }

    public String getCacheVoiceRoom(Integer userId){
        Object vRoom=HallServer.redisUtil.hget(CommonConstants.USER + userId, CommonConstants.USER_VOICE_ROOM);
        if(Objects.nonNull(vRoom)){
            return vRoom.toString();
        }
        return "";
    }
}
